package com.example.ui_view;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ListViewActivity extends AppCompatActivity {
    private ListView listView;
    int[] pic = new int[]{R.mipmap.img01,R.mipmap.img02,R.mipmap.img03,R.mipmap.img04,R.mipmap.img05,R.mipmap.img06
    ,R.mipmap.img01,R.mipmap.img02,R.mipmap.img03,R.mipmap.img04,R.mipmap.img05,R.mipmap.img06,R.mipmap.img01,R.mipmap.img02,R.mipmap.img03,R.mipmap.img04,R.mipmap.img05,R.mipmap.img06
            ,R.mipmap.img01,R.mipmap.img02,R.mipmap.img03,R.mipmap.img04,R.mipmap.img05,R.mipmap.img06};
    String[] str = new String[]{"1111","2222","3333","4444","5555","6666","1111","2222","3333","4444","5555","6666","1111","2222","3333","4444","5555","6666","1111","2222","3333","4444","5555","6666"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view);
        listView = findViewById(R.id.listView);
        List<Map<String,Object>> list = new ArrayList<>();
        for (int i = 0;i<pic.length;i++){
            Map<String,Object> map = new HashMap<>();
            map.put("myIma",pic[i]);
            map.put("myText",str[i]);
            list.add(map);
        }
        String[] from = new String[]{"myIma","myText"};
        int[] to = new int[]{R.id.img_contact,R.id.tv_contact};
        ContactSimpleAdapter contactSimpleAdapter = new ContactSimpleAdapter(ListViewActivity.this,list,R.layout.list_item,from,to);
        //SimpleAdapter adapter = new SimpleAdapter(ListViewActivity.this,list,R.layout.list_item,from,to);
        listView.setAdapter(contactSimpleAdapter);
    }
    class ContactSimpleAdapter extends SimpleAdapter{
        Context context;
        int resource;
        public ContactSimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
            super(context, data, resource, from, to);
            this.context = context;
            this.resource = resource;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = LayoutInflater.from(context).inflate(resource,parent,false);
            TextView textView = convertView.findViewById(R.id.tv_contact);
            if(position%6==0){

                textView.setTextColor(Color.RED);
            }else {
                textView.setTextColor(Color.GRAY);
            }
            return super.getView(position, convertView, parent);
        }
    }
}
